<?php


namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Upload;
use think\Config;
use think\Db;
use think\Request;

/**
 * 示例接口
 */
class Video extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    //用户身份
    public function usergroup(){

        $groupid=$this->auth->group_id;
        $this->success('返回成功', ['groupid' => $groupid]);

        die;
    }

    /**
     * 分类信息
     */
    public function alltest(){

        $groupid=$this->auth->group_id;

        $list=Db::name('video_cate')
            ->where('user_group_ids','like','%'.$groupid.'%')
            ->select();

        foreach ($list as &$key){

            $key['image']=cdnurl($key['image'],true);

        }

        $this->success('分类信息',$list);

    }


    //我能学习的内容

    public function getmyvideo(){

        $groupid=$this->auth->group_id;
        $user=$this->auth->getUser();

        $key=$this->request->param('key');
        if  ($key){
            $map['a.name']=['like','%'.$key.'%'];
        }

        $cid=$this->request->param('cid');

        if  ($cid){
            $map['a.videos_cate_id']=['eq',$cid];
        }

        $map['b.user_group_ids']=['like','%'.$groupid.'%'];


        $list=Db::name('videos')
            ->alias('a')
            ->join('fa_video_cate b' ,'a.videos_cate_id=b.id')
            ->where($map)
            ->field('a.*')
            ->select();




        foreach ($list as &$key){

            $key['image']=cdnurl($key['image'],true);
            $key['bi']=0;


            $logs['v_id']=$key['id'];
            $logs['user_id']=$user['id'];

            $find=Db::name('video_log')
                ->where($logs)
                ->find();

            if ($find){
                $key['bi']='100';
            }else{
                $key['bi']=0;
            }


        }

        $this->success('返回成功',$list);

    }

    //新的学习内容

    public function getmynewvideo(){
        $groupid=$this->auth->group_id;
        $user=$this->auth->getUser();

        $map['a.news']=['eq','1'];
        $map['b.user_group_ids']=['like','%'.$groupid.'%'];


        $list=Db::name('videos')
            ->alias('a')
            ->join('fa_video_cate b' ,'a.videos_cate_id=b.id')
            ->where($map)
            ->field('a.*')
            ->select();


        foreach ($list as &$key){

            $key['image']=cdnurl($key['image'],true);
            $key['bi']=0;


            $logs['v_id']=$key['id'];
            $logs['user_id']=$user['id'];

            $find=Db::name('video_log')
                ->where($logs)
                ->find();

            if ($find){
                $key['bi']='100';
            }else{
                $key['bi']=0;
            }


        }

        $this->success('返回成功',$list);


    }

    //详情信息
    public function getvideobyid(){

        $id=$this->request->param('id');
        $user=$this->auth->getUser();


        $list=Db::name('videos')->where(['id'=>$id])->find();

        $vidoe=$list['files'];

        $vidoes=explode(',',$vidoe);

        foreach ($vidoes as $li=>$v){
            $vidoes[$li]=cdnurl($v,true);
        }

        if ($vidoe){
            $list['videos']=$vidoes;
        }else{
            $list['videos']=[];
        }





        $logs['user_id']=$user['id'];
        $logs['v_id']=$id;
        $find=Db::name('video_log')->where($logs)->find();

        if (!$find){
            $logs['all']=1;
            $logs['text']=0;
            $logs['times']=time();

            Db::name('video_log')->data($logs)->insert();
        }

        $this->success('返回成功',$list);
    }


    //测试内容
    public  function gettestbyid(){

        $id=$this->request->param('id');

        $user=$this->auth->getUser();

        $find=Db::name('videos_test')
            ->where(['videos_id'=>$id])
            ->select();

        $vi=Db::name('videos')->where(['id'=>$id])->find();

       foreach ($find as &$item){
           $item['aimage']=cdnurl($item['aimage'],true);
           $item['bimage']=cdnurl($item['bimage'],true);
           $item['cimage']=cdnurl($item['cimage'],true);
           $item['dimage']=cdnurl($item['dimage'],true);
       }

        $show=0;

        $log=Db::name('video_log')->where(['user_id'=>$user['id'],'v_id'=>$id])->find();

        $show=$log['score']>0?'1':'0';
        $score=$log['score'];
        $this->success($vi['name'],['list'=>$find,'show'=>$show,'score'=>$score]);

    }

    /**
     * 用户提交得分
     */

    public function submitdf(){

        $id=$this->request->param('id');

        $user=$this->auth->getUser();

        $number=$this->request->param('number');

        Db::name('video_log')->where(['v_id'=>$id])
            ->where(['user_id'=>$user['id']])
            ->update(['score'=>$number]);

        $this->success('提交成功！');

    }

    //考试信息
    public function gettest(){

        $groupid=$this->auth->group_id;


        $map['b.user_group_ids']=['like','%'.$groupid.'%'];


        $list=Db::name('videos')
            ->alias('a')
            ->join('fa_video_cate b' ,'a.videos_cate_id=b.id')
            ->where($map)
            ->field('a.*')
            ->select();


        foreach ($list as &$key){
            $key['image']=cdnurl($key['image'],true);
        }

        $this->success('提交成功！',$list);

    }

}